"""
@Author  : 康帅
@Time    : 2021/5/20 14:29
@Function: socketio客户端
"""


import socketio


def create_client():
    sio = socketio.Client()

    @sio.event
    def connect():
        print('connection established')
        sio.emit('client', {'foo': 'bar'})

    @sio.on('serve')
    def on_message(data):
        print('client received a message!', data)

    # @sio.event
    # def message(data):
    #     print('message received with ', data)
    #     sio.emit('client', {'response': 'my response'})

    @sio.event
    def connect_error():
        print("The connection failed!")
        sio.disconnect()

    @sio.event
    def disconnect():
        print('disconnected from server')
        sio.disconnect()

    sio.connect('http://localhost:5000')
    sio.wait()


if __name__ == '__main__':
    create_client()
